package com.bcts.rrh.drawingboard.shapeview;

import android.graphics.Color;
import android.opengl.Matrix;
import android.util.Log;

import com.bcts.rrh.drawingboard.utils.VaryTools;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;

import javax.microedition.khronos.opengles.GL10;

import static com.bcts.rrh.drawingboard.Constants.ANGLE_SPAN;
import static com.bcts.rrh.drawingboard.Constants.TOUCH_SCALE_FACTOR;
import static com.bcts.rrh.drawingboard.Constants.UNIT_SIZE;

public class Circle extends Shape_3d{
    //普通圆，可做圆柱底部
    private FloatBuffer mVertexBuffer;//顶点坐标数据缓冲
    private FloatBuffer myNormalBuffer;//法向量缓冲
    private FloatBuffer mTextureBuffer;//顶点纹理数据缓冲
    int vCount=0;//顶点数量
    float scale;//尺寸
    float scalezoom;//缩放尺寸
    float mAngleY;//转动角度
    float mAngleZ;//绕Z轴旋转
    int texId;//纹理ID
    int color;
    float[] tempVerteices;
    ArrayList<Float> inittempVerteices;//初始的坐标点数组
    float minX;//x轴最小位置
    float maxX;//x轴最大位置
    float minY;//y轴最小位置
    float maxY;//y轴最大位置
    float minZ;//z轴最小位置
    float maxZ;//z轴最大位置
    float midX;//中心点坐标
    float midY;
    float midZ;
    float xOffset;//在轴上移动的位置
    float yOffset;
    float zOffset;

    public Circle(float scale,int texId,int color,float xOffset,float yOffset,float zOffset)
    {
        this.scale=scale;
        this.scalezoom=scale;
        this.texId=texId;
        this.color=color;
        this.xOffset=xOffset;
        this.yOffset=yOffset;
        this.zOffset=zOffset;
        int spannum=(int)(360.0f/ANGLE_SPAN);
        vCount=3*spannum;//顶点个数,共有n个三角形，每个三角形都有3个顶点

        float[] vertices=new float[vCount*3];//顶点坐标数据
        float[] textures=new float[vCount*2];//顶点纹理S,T坐标值数组

        //坐标数据初始化
        int count=0;
        int stCount=0;
        for (float angdeg=0;Math.ceil(angdeg)<360;angdeg+=ANGLE_SPAN){
            double angrad=Math.toRadians(angdeg);//当前弧度
            double angradNext=Math.toRadians(angdeg+ANGLE_SPAN);//下一弧度

            //中心点
            vertices[count++]=0;//顶点坐标
            vertices[count++]=0;
            vertices[count++]=0;

            textures[stCount++]=0.5F;//ST坐标
            textures[stCount++]=0.5f;

            //当前点
            vertices[count++]=(float)(-scale*UNIT_SIZE*Math.sin(angrad));//顶点坐标
            vertices[count++]=(float)(scale*UNIT_SIZE*Math.cos(angrad));
            vertices[count++]=0;

            textures[stCount++]=(float)(0.5-0.5*Math.sin(angrad));//st坐标
            textures[stCount++]=(float)(0.5-0.5*Math.cos(angrad));

            //下一点
            vertices[count++]=(float)(-scale*UNIT_SIZE*Math.sin(angradNext));//顶点坐标
            vertices[count++]=(float)(scale*UNIT_SIZE*Math.cos(angradNext));
            vertices[count++]=0;

            textures[stCount++]=(float)(0.5-0.5*Math.sin(angradNext));//st坐标
            textures[stCount++]=(float)(0.5-0.5*Math.cos(angradNext));
        }
        inittempVerteices=new ArrayList<Float>();//存放顶点坐标的ArrayList
        for (int i=0;i<vertices.length;i++){
            inittempVerteices.add(vertices[i]);
        }
        tempVerteices=pointMatrix();
        ByteBuffer vbb = ByteBuffer.allocateDirect(tempVerteices.length*4);//创建顶点坐标数据缓冲
        vbb.order(ByteOrder.nativeOrder());//设置字节顺序为本地操作系统顺序
        mVertexBuffer = vbb.asFloatBuffer();//转换为float型缓冲
        mVertexBuffer.put(tempVerteices);//向缓冲区中放入顶点坐标数据
        mVertexBuffer.position(0);//设置缓冲区起始位置
        //法向量数据初始化-用于计算光照
        float[] normals=new float[vertices.length];
        for(int i=0;i<normals.length;i+=3){
            normals[i]=0;
            normals[i+1]=0;
            normals[i+2]=1;
        }

        ByteBuffer nbb = ByteBuffer.allocateDirect(normals.length*4);//创建顶点法向量数据缓冲
        nbb.order(ByteOrder.nativeOrder());//设置字节顺序为本地操作系统顺序
        myNormalBuffer = nbb.asFloatBuffer();//转换为float型缓冲
        myNormalBuffer.put(normals);//向缓冲区中放入顶点法向量数据
        myNormalBuffer.position(0);//设置缓冲区起始位置

        //纹理坐标数据初始化
        ByteBuffer cbb = ByteBuffer.allocateDirect(textures.length*4);//创建顶点纹理数据缓冲
        cbb.order(ByteOrder.nativeOrder());//设置字节顺序为本地操作系统顺序
        mTextureBuffer = cbb.asFloatBuffer();//转换为float型缓冲
        mTextureBuffer.put(textures);//向缓冲区中放入顶点纹理数据
        mTextureBuffer.position(0);//设置缓冲区起始位置

    }
    private void test(){
        //创建绘制顶点数据缓冲
        ByteBuffer vbb = ByteBuffer.allocateDirect(tempVerteices.length*4);
        vbb.order(ByteOrder.nativeOrder());//设置字节顺序
        //mVertexBuffer.clear();
        mVertexBuffer = vbb.asFloatBuffer();//转换为float型缓冲
        mVertexBuffer.put(tempVerteices);//向缓冲区中放入顶点坐标数据
        mVertexBuffer.position(0);//设置缓冲区起始位置
    }
    //点缓存的坐标变换，按矩阵变换来变换
    private float[] pointMatrix(){
        //1.计算坐标变换矩阵
        VaryTools tools=new VaryTools();
        tools.pushMatrix();
        tools.translate(xOffset, yOffset, zOffset);
        tools.rotate(mAngleY, 0, 1, 0);
        tools.rotate(mAngleZ,0,0,1);
        tools.scale(scalezoom, scalezoom, scalezoom);
        float[] mMatrixCurrent=tools.getmMatrixCurrent();
        tools.popMatrix();
        float vertices[]=new float[inittempVerteices.size()];
        for(int i=0;i<inittempVerteices.size();i++)
        {
            vertices[i]=inittempVerteices.get(i);
        }
        //float[] tempVerteices=inittempVerteices;//将点坐标变换为初始状态的坐标重新计算
        for(int i=0;i<vertices.length/3;i++){
            float[] pointMx={vertices[i*3],vertices[i*3+1],vertices[i*3+2],1};
            Matrix.multiplyMV(pointMx,0,mMatrixCurrent,0,pointMx,0);
            vertices[i*3]=pointMx[0];
            vertices[i*3+1]=pointMx[1];
            vertices[i*3+2]=pointMx[2];
        }
        return vertices;
    }
    @Override
    public void drawSelf(GL10 gl) {
        int red=Color.red(color);
        int green=Color.green(color);
        int blue=Color.blue(color);
        int alpha=Color.alpha(color);
        //gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
        gl.glColor4f(red/255.0f,green/255.0f,blue/255.0f, alpha/255.0f);
//        gl.glTranslatef(xOffset, yOffset, zOffset);
//        gl.glRotatef(mAngleY, 0, 1, 0);
//        gl.glRotatef(mAngleZ,0,0,1);
//        if(true)
//        {
//            gl.glScalef(scalezoom, scalezoom, scalezoom);
//        }
        //允许使用顶点数组
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        //为画笔指定顶点坐标数据
        gl.glVertexPointer
                (
                        3,				//每个顶点的坐标数量为3  xyz
                        GL10.GL_FLOAT,	//顶点坐标值的类型为 GL_FIXED
                        0, 				//连续顶点坐标数据之间的间隔
                        mVertexBuffer	//顶点坐标数据
                );

        if (this.texId>0){
            //开启纹理
            gl.glEnable(GL10.GL_TEXTURE_2D);
            //允许使用纹理ST坐标缓冲
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
            //为画笔指定纹理ST坐标缓冲
            gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer);
            //绑定当前纹理
            gl.glBindTexture(GL10.GL_TEXTURE_2D, texId);
        }
        //绘制图形
        gl.glDrawArrays
                (
                        GL10.GL_TRIANGLES, 		//以三角形方式填充
                        0, 			 			//开始点编号
                        vCount					//顶点数量
                );
        if (this.texId>0){
            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);//禁用纹理数组
            gl.glDisable(GL10.GL_TEXTURE_2D);//关闭纹理
        }

    }

    @Override
    public float[] findMinMax() {
        // TODO Auto-generated method stub
        //最大最小值初始化
        minX=tempVerteices[0];//x轴最小位置
        maxX=tempVerteices[0];//x轴最大位置
        minY=tempVerteices[1];//y轴最小位置
        maxY=tempVerteices[1];//y轴最大位置
        minZ=tempVerteices[2];//z轴最小位置
        maxZ=tempVerteices[2];//z轴最大位置
        for(int i=0;i<tempVerteices.length/3;i++)
        {
            //判断X轴的最小和最大位置
            if(tempVerteices[i*3]<minX)
            {
                minX=tempVerteices[i*3];
            }
            if(tempVerteices[i*3]>maxX)
            {
                maxX=tempVerteices[i*3];
            }
            //判断Y轴的最小和最大位置
            if(tempVerteices[i*3+1]<minY)
            {
                minY=tempVerteices[i*3+1];
            }
            if(tempVerteices[i*3+1]>maxY)
            {
                maxY=tempVerteices[i*3+1];
            }
            //判断Z轴的最小和最大位置
            if(tempVerteices[i*3+2]<minZ)
            {
                minZ=tempVerteices[i*3+2];
            }
            if(tempVerteices[i*3+2]>maxZ)
            {
                maxZ=tempVerteices[i*3+2];
            }
        }
        Log.d("Ball minX="+minX,"maxX="+(maxX-minX));
        Log.d("Ball minY="+minY,"maxY="+(maxY-minY));
        Log.d("Ball minZ="+minZ,"maxZ="+(maxZ-minZ));
        float[] length={maxX-minX,maxZ-minZ,maxY-minY};

        return length;
    }

    @Override
    public float[] findMid() {
        // TODO Auto-generated method stub
        midX=(minX+maxX)/2;
        midY=(minY+maxY)/2;
        midZ=(minZ+maxZ)/2;
        Log.d("Ball midX=",""+midX);
        Log.d("Ball midY=",""+midY);
        Log.d("Ball midZ=",""+midZ);
        float[] mid={midX,midY,midZ};
        return mid;
    }

    @Override
    public void setMove(boolean flag) {//移动
        this.moveFlag=flag;
        Log.d("cube",""+this.moveFlag);
    }
    public void setRotate(boolean flag) {//移动
        this.rotateFlag=flag;
        Log.d("cube",""+this.rotateFlag);
    }
    @Override
    public void setHilight(boolean flag) {//缩放
        this.hiFlag=flag;
        Log.d("cube",""+this.hiFlag);
    }

    @Override
    public void settexture(int texId) {
        if(texId>0){//如果纹理存在
            this.texId=texId;
        }
    }

    @Override
    public boolean getMove() {
        return this.moveFlag;
    }

    @Override
    public boolean getRotate() {
        return this.rotateFlag;
    }

    @Override
    public boolean getHilight() {
        return this.hiFlag;
    }

    @Override
    public void Move(float dx, float dy) {
        xOffset=dx;
        yOffset=dy;
        tempVerteices=pointMatrix();
        test();
    }


    @Override
    public void Rotate(float dx, float dy,float ox,float oy) {
        this.mAngleY+=dx*TOUCH_SCALE_FACTOR;//设置延y轴旋转角度
        this.mAngleZ+=dy*TOUCH_SCALE_FACTOR;//设置沿z轴旋转角度
        tempVerteices=pointMatrix();
        test();
    }

    @Override
    public void Hilight(float scachange) {
        if(scachange>0){
            this.scalezoom+=0.01f;
        }
        else {
            if(this.scalezoom>=0.2f){
                this.scalezoom-=0.01f;
            }
        }
        tempVerteices=pointMatrix();
        test();
    }

    @Override
    public void setpoint(int pointid, float dx, float dy) {

    }

    @Override
    public void setcolor(int color) {
        this.color=color;
    }

    @Override
    public void meActionup() {

    }
}
